import LocalValue from "./LocalValue";
import LocalStorage from "./LocalStorage";
export default class LocalList extends LocalValue {


    protected value: any[];

    get(index: number) {
        if (index >= 0 && index <= this.value.length - 1) {
            return this.value[index];
        } else {
            // this.value[index] = 0;
            this.set(index, 0)
            return this.value[index];
        }
    }
    protected loadValue(initValue: any[]) {
        this.value = []
        if (this.readLocal) {
            let localValue = LocalStorage.getJsonBase64(this.key);
            cc.log(' localValue ',localValue)
            cc.log('key ===  ',this.key)
            if (localValue == null || localValue == undefined) {
                this.setValue(initValue);
            } else {// 当更新后的长度有变化时的处理。始终保持与给定数组的长度一直。数值保留存档数值，不够的用初始数值补充。
                if (localValue.length != initValue.length) {
                    for (let index = 0; index < initValue.length; index++) {
                        if (index <= localValue.length - 1) {
                            this.value[index] = localValue[index];
                        } else {
                            this.value[index] = initValue[index]
                        }
                    }
                } else {
                    this.value = localValue;
                }

            }

        } else {
            this.value = initValue;
        }
    }
    size() {
        return this.value.length;
    }

    set(index: number, value: any) {
        // if(index >=0 && index <= this.value.length - 1){
        this.value[index] = value;
        this.saveValue();
        // }  
    }

}
